package com.ruoyi.project.product.series.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.series.domain.Series;
import com.ruoyi.project.product.series.service.ISeriesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品系列操作处理
 * @author Mai
 */
@Controller
@RequestMapping("product/series")
public class SeriesController extends BaseController {

    private String prefix="product/series";

    @Autowired
    private ISeriesService seriesService;

    /**
     * 跳转到查询页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("product:series:view")
    public String series(){
        return prefix + "/series";
    }

    /**
     * 产品系列查询
     * @param series
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("product:series:list")
    public TableDataInfo list(Series series){
        startPage();
        List<Series> list = seriesService.selectSeriesList(series);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(){
        return prefix + "/add";
    }

    /**
     * 产品系列新增
     * @param series
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("product:series:add")
    public AjaxResult addSave(Series series){
        //判断1是否等于返回的值，如果是等于1 ，就表中存在，不能添加数据。
        if(UserConstants.SERIES_NAME_NOT_UNIQUE.equals(seriesService.checkSeriesNameUnique(series))){
            return error("新增系列"+series.getSeriesName()+"失败，系列名称已存在");
        }
        return toAjax(seriesService.insertSeries(series));
    }

    /**
     * 系列修改
     * @param seriesId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{seriesId}")
    public String edit(@PathVariable("seriesId") Long seriesId, ModelMap mmap){
        mmap.put("series",seriesService.selectSeriesById(seriesId));
        return prefix + "/edit";
    }

    /**
     * 系列修改保存
     * @param series
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions("product:series:edit")
    public AjaxResult editSave(Series series){
        return toAjax(seriesService.updateSeries(series));
    }



    /**
     * 系列批量删除操作
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("product:series:remove")
    public AjaxResult remove(String ids){
        try {
            return toAjax(seriesService.deleteSeriesByIds(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 校验系列名称是否存在
     * @param series
     * @return
     */
    @ResponseBody
    @PostMapping("/checkSeriesNameUnique")
    public String checkSeriesNameUnique(Series series){
        return seriesService.checkSeriesNameUnique(series);
    }

}
